package com.ztc.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EasyUITreeUtil {

	/**
	 * 递归生成easyui tree
	 * 封装成id, text:name, children的格式，easyui tree格式
	 * @param sourceList
	 * @param resultList
	 * @param id
	 * @param pid
	 * @param fieldsArr
	 * @return
	 */
	public static List<Map<String, Object>> loopForTree(List<Map<String, Object>> sourceList,
			List<Map<String, Object>> resultList, String id, String pid, String order, String text,
			String[] fieldsArr) {
		List<Map<String, Object>> myMapList = new ArrayList<Map<String, Object>>();
		
		/*
		 * 首先过滤出纯粹子节点科目
		 */
		List<Map<String, Object>> parentList = new ArrayList<Map<String, Object>>();
		List<Map<String, Object>> childList = new ArrayList<Map<String, Object>>();
		
		for (Map<String, Object> fone : sourceList) {
			String fid = fone.get(id) + "";
			String fparentId = fone.get(pid) + "";

			boolean isParent = false;
			boolean isChild = false;
			for (Map<String, Object> cone : sourceList) {
				String cid = cone.get(id) + "";
				String cparentId = cone.get(pid) + "";
				
				if (fid.equals(cparentId)) {
					isParent = true;
				}
				if (fparentId.equals(cid)) {
					isChild = true;
				}
			}
			
			//双层循环遍历权限列表，区分其中哪些权限是别人的子权限，哪些权限是别人的父权限
			//因为数据嵌套多层，过滤出的这两类数据必然会有重合的地方，但是childList中有而parentList没有的数据必然是叶子节点
			if (isParent) {
				parentList.add(fone);
			}
			if (isChild || (!isParent && !isChild)) {
				childList.add(fone);
			}
		}
		
		//childList中有而parentList没有的数据必然是叶子节点
		List<Map<String, Object>> leafList = new ArrayList<Map<String, Object>>();
		
		for (Map<String, Object> cone : childList) {
			String cid = cone.get(id) + "";
			boolean exist = false;
			for (Map<String, Object> fone : parentList) {
				String fid = fone.get(id) + "";
				if (cid.equals(fid)) {
					exist = true;
					break;
				}
			}
			if (!exist) {
				leafList.add(cone);
			}
		}
		
		
		/*
		 * 为最后的结果字段赋值
		 * leafList表示当前循环中的叶子权限
		 * 但是相对于上一层传过来的resultList，leafList中有些数据为resultList中数据的父权限
		 * 因此，此处对比出leafList中每个权限的子权限，然后为相应字段赋值
		 * 
		 * 如果数据在resultList存在且在leafList中找不到任何父权限，则说明此数据层级也为当前循环的叶子权限
		 */
		for (Map<String, Object> leaf : leafList) {
			List<Map<String, Object>> children = new ArrayList<Map<String, Object>>();
			
			for (Map<String, Object> one : resultList) {
				String parentId = one.get(pid) + "";
				
				if (parentId.equals(leaf.get(id) + "")) {
					children.add(one);
				}
			}
			
			Collections.sort(children, (Map<String, Object> o1, Map<String, Object> o2) -> {
				int o1sequence = (Integer) o1.get(order);
				int o2sequence = (Integer) o2.get(order);
        		return o1sequence - o2sequence;
			});
			
			Map<String, Object> myMap = new HashMap<String, Object>();
			for(String key : fieldsArr) {
				myMap.put(key, leaf.get(key));
			}
			myMap.put("id", leaf.get(id));
			myMap.put(pid, leaf.get(pid));
			myMap.put("name", leaf.get(text));
			myMap.put("text", leaf.get(text));
			myMap.put(order, leaf.get(order));
			if(leaf.get("children") != null) {
				children.addAll((List<Map<String, Object>>)leaf.get("children"));
			}
			myMap.put("children", children);
			if (children.size() > 0) {
				myMap.put("iconCls", "icon-parent");
			} else {
				myMap.put("iconCls", "icon-final");
			}
			myMapList.add(myMap);
		}
		for (Map<String, Object> one : resultList) {
			boolean exist = false;
			for (Map<String, Object> leaf : leafList) {
				String parentId = one.get(pid) + "";
				if (parentId.equals(leaf.get(id) + "")) {
					exist = true;
					break;
				}
			}
			
			if (!exist) {
				myMapList.add(one);
			}
		}
		
		if (parentList.size() > 0) {
			sourceList.removeAll(leafList);
			myMapList = loopForTree(sourceList, myMapList, id, pid, order, text, fieldsArr);
		}
		Collections.sort(myMapList, (Map<String, Object> o1, Map<String, Object> o2) -> {
			int o1sequence = (Integer) o1.get(order);
			int o2sequence = (Integer) o2.get(order);
    		return o1sequence - o2sequence;
		});
		return myMapList;
	}
}
